package org.qpcr.community.utils;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.util.Scanner;

public class WebSocketExample {

    public static void main(String[] args) {
        try {
//            URI uri = new URI("ws://127.0.0.1:8848/messaging/privatekey20240904"); // 替换为你的WebSocket服务器URI
            URI uri = new URI("ws://36.133.145.167:8848/messaging/privatekey20240904"); // 替换为你的WebSocket服务器URI
            WebSocketClient webSocketClient = new WebSocketClient(uri) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    System.out.println("新连接已打开");
//                    send("{\"id\":\"event-status\",\"topic\":\"/device/httppro/httpdev/message/event/event1\",\"parameter\":{\"deviceId\":\"httpdev\"},\"type\":\"ping\"}"); // 发送消息
//                    send("{\"id\":\"event-login\",\"topic\":\"/device/QPCRFAST001/79b982f295eb01b2/message/event/eventAll\",\"parameter\":{\"deviceId\":\"79b982f295eb01b2\"},\"type\":\"sub\"}"); // 发送消息
//                    send("{\"id\":\"event-login\",\"topic\":\"/device/QPCRFAST001/79b982f295eb01b2/message/event/Carrier\",\"parameter\":{\"deviceId\":\"79b982f295eb01b2\"},\"type\":\"sub\"}"); // 扫码载体
//                    send("{\"id\":\"event-login\",\"topic\":\"/device/QPCRFAST001/PCR001-TEST/message/event/moduleSate\",\"parameter\":{\"deviceId\":\"PCR001-TEST\"},\"type\":\"sub\"}"); // 状态

                    send("{\"id\":\"keep-device-online\",\"topic\":\"/realtime/keep-device-online\",\"parameter\":{\"productId\":\"QPCRFAST001\",\"deviceId\":\"796b527ec0c3c1e6\"},\"type\":\"sub\"}");//心跳
                }

                @Override
                public void onMessage(String message) {
                    System.out.println("接收到消息: " + message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    System.out.println("连接已关闭");
                }

                @Override
                public void onError(Exception ex) {
                    ex.printStackTrace();
                }
            };

            webSocketClient.connect(); // 连接到WebSocket服务器
            try (Scanner scanner = new Scanner(System.in)) {
                while (true) {
                    String userQuery = scanner.nextLine();
                    webSocketClient.send(userQuery);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
